package com.ljx.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljx.JwtUtils;
import com.ljx.R;
import com.ljx.client.OrdersClient;
import com.ljx.pojo.EduCourse;
import com.ljx.pojo.chapter.ChapterVo;
import com.ljx.pojo.frontVo.CourseFrontVo;
import com.ljx.pojo.frontVo.CourseWebVo;
import com.ljx.pojo.vo.CourseInfoVo;
import com.ljx.service.impl.EduChapterServiceImpl;
import com.ljx.service.impl.EduCourseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 李加喜
 * @date 2020/10/1 0001 15:38
 * @Email 1129071273@qq.com
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {
    @Autowired
    private EduCourseServiceImpl courseService;
    @Autowired
    private EduChapterServiceImpl chapterService;
    @Autowired
    private OrdersClient ordersClient;
    //分页查询课程的详细信息
    @PostMapping("/getFrontCourseList/{current}/{limit}")
    public R getFrontCourseList(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> page = new Page<>(current, limit);
        Map<String,Object> map= courseService.getCourseInfoList(page,courseFrontVo);
        return R.ok().data(map);
    }
    //根据课程的ID查询课程的详细信息
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterById(courseId);
        //根据课程id和用户id查询当前课程是否已经支付过了
        String id = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseWebVo",courseWebVo)
                .data("chapterVideoList",chapterVideoList)
                .data("isBuy",buyCourse);
    }
}
